package com.ctg.itrdc.sysmgr.portal.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ctg.itrdc.pasp.context.service.BaseService;
import com.ctg.itrdc.sysmgr.portal.api.dto.PrivFuncRelDTO;
import com.ctg.itrdc.sysmgr.portal.api.dto.PrivilegeDTO;
import com.ctg.itrdc.sysmgr.portal.api.service.IPrivFuncRelService;
import com.ctg.itrdc.sysmgr.portal.data.IPrivFuncRelDao;
import com.ctg.itrdc.sysmgr.portal.data.IPrivilegeDao;
import com.ctg.itrdc.sysmgr.portal.data.entity.PrivFuncRel;
import com.ctg.itrdc.sysmgr.portal.data.entity.Privilege;
import com.ctg.itrdc.sysmgr.portal.service.utils.BeanUtils;
import com.google.common.collect.Lists;

@Service("privFuncRelService")
public class PrivFuncRelServiceImpl extends BaseService<PrivFuncRel, Long, PrivFuncRelDTO>
    implements IPrivFuncRelService {

	@Resource
	private IPrivFuncRelDao privFuncRelDao;
	@Resource
	private IPrivilegeDao privilegeDao;
	
	@Transactional
	@Override
	public void deletePrivAndPrivFuncRel(PrivFuncRelDTO privFuncRel) {
		if(privFuncRel==null) return;
		List<PrivFuncRelDTO> privFuncRelList = super.selectByExample(privFuncRel);
		List<PrivilegeDTO> privilegeList = Lists.newArrayList();
		for (PrivFuncRelDTO privFuncRelDTO : privFuncRelList) {
			Long privId = privFuncRelDTO.getPrivId();
			PrivilegeDTO privilege = new PrivilegeDTO();
			privilege.setPrivId(privId);
			privilegeList.add(privilege);
		}
		// 删除priv_func_rel
		if(CollectionUtils.isNotEmpty(privFuncRelList)) {
			List<PrivFuncRel> entities = BeanUtils.copyListNotException(privFuncRelList, PrivFuncRel.class);
			privFuncRelDao.jdbcBatchDeleteByPrimary(entities);
		}

		// 删除 privilege
		if(CollectionUtils.isNotEmpty(privilegeList)) {
			List<Privilege> entities = BeanUtils.copyListNotException(privilegeList, Privilege.class);
			privilegeDao.jdbcBatchDeleteByPrimary(entities);
		}
	}
}
